#!/bin/sh
#
# /etc/init.d/ntropa -- startup script for the Ntropa HTML templating daemon
#
# Written by Miquel van Smoorenburg <miquels@cistron.nl>.
# Modified for Debian GNU/Linux	by Ian Murdock <imurdock@gnu.ai.mit.edu>.
# Modified for Tomcat by Stefan Gybas <sgybas@debian.org>.
# Modifed for ntropa by Janek Bogucki <janekdb@gmail.com>
#
### BEGIN INIT INFO
# Provides:          ntropa
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Should-Start:      $named
# Should-Stop:       $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start Ntropa.
# Description:       Start the Ntropa HTML templating daemon.
### END INIT INFO

set -e

PATH=/bin:/usr/bin:/sbin:/usr/sbin
NAME=ntropa
DESC="Ntropa HTML templating daemon"
DEFAULT=/etc/default/$NAME

. /lib/lsb/init-functions
. /etc/default/rcS

# The following variables can be overwritten in $DEFAULT

# Run Ntropa as this user ID
NTROPA_USER=ntropa

# The installation directory
NTROPA_BASE="/var/lib/ntropa"

# End of variables that can be overwritten in $DEFAULT

# overwrite settings from default file
if [ -f "$DEFAULT" ]; then
	. "$DEFAULT"
fi

DAEMON=$NTROPA_BASE/bin/catalina.sh

test -f $DAEMON || exit 0

[ -z "$NTROPA_USER" ] && NTROPA_USER=ntropa

# Define other required variables
# Only root can create files under /var/run so rely on the PID
# file created by catalina.sh
#NTROPA_PID="/var/run/$NAME.pid"
NTROPA_PID="$NTROPA_BASE/home/ntropa.pid"

case "$1" in
  start)
	log_daemon_msg "Starting $DESC" "$NAME"
	if start-stop-daemon --test --start --pidfile "$NTROPA_PID" \
		--user $NTROPA_USER --startas "$DAEMON start" \
		>/dev/null; then
echo "start: 1"
		sudo -u $NTROPA_USER $DAEMON start

	else
echo "start: 2"
	        log_progress_msg "(already running)"
	fi
	log_end_msg 0
	;;
  stop)
        #TODO
	log_daemon_msg "Stopping $DESC" "$NAME"
        if start-stop-daemon --test --start --pidfile "$NTROPA_PID" \
		--user "$NTROPA_USER" --startas "$DAEMON start" \
		>/dev/null; then
echo "stop: 1"
		log_progress_msg "(not running)"
	else
echo "stop: 2"
		sudo -u $NTROPA_USER $DAEMON stop
                #TODO: Remove PID file?
	fi
	log_end_msg 0
	;;
   status)
        if start-stop-daemon --test --start --pidfile "$NTROPA_PID" \
		--user $NTROPA_USER --startas "$DAEMON start" \
		>/dev/null; then

		if [ -f "$NTROPA_PID" ]; then
		    log_success_msg "$DESC is not running, but pid file exists."
		    exit 1
		else
		    log_success_msg "$DESC is not running."
		    exit 3
		fi
	else
		log_success_msg "$DESC is running with pid `cat $NTROPA_PID`"
		exit 0
	fi
        ;;
  restart|force-reload)
echo "restart: 1"
        if start-stop-daemon --test --stop --pidfile "$NTROPA_PID" \
		--user $NTROPA_USER --startas "$DAEMON start" \
		>/dev/null; then
echo "restart: 2"
echo '$0: ' $0
		$0 stop
echo "restart: 2.1"
		sleep 10
	fi
echo "restart: 3"
	$0 start
echo "restart: 4"
	;;
  try-restart)
echo "try-restart: 1"
        if start-stop-daemon --test --start --pidfile "$NTROPA_PID" \
		--user $NTROPA_USER --startas "$DAEMON start" \
		>/dev/null; then
		$0 start
echo "try-restart: 2"
	fi
        ;;
  *)
	log_success_msg "Usage: $0 {start|stop|restart|try-restart|force-reload|status}"
	exit 1
	;;
esac

exit 0
